/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.editor.model;

import javax.swing.text.Element;
import javax.swing.text.html.HTML.Attribute;
import javax.swing.text.html.HTML.Tag;
import offset.nodes.Constants;
import offset.nodes.client.model.HttpUtils;
import offset.nodes.client.editor.Editor;
import offset.nodes.client.editor.view.hyperlink.ExternalHyperlinkInfo;
import offset.nodes.client.editor.view.hyperlink.InternalHyperlinkInfo;

/**
 *
 * @author Walter Luetgenau
 */
public class HyperlinkModel extends EditorModel {

    public static final String NODE_REFERENCE = Constants.PAR_NODE_REFERENCE;
    public static final String DATA_REFERENCE = Constants.PAR_DATA_REFERENCE;
    public static final String REPOSITORY_URL =
            Constants.PATH_SEPARATOR + Constants.CONTEXTMAPPING_NODES + Constants.PATH_SEPARATOR + Constants.SERVLETMAPPING_REPOSITORY;
    
    Editor editor;

    public HyperlinkModel(DocumentContext context, Editor editor) {
        super(context);
        this.editor = editor;
    }

    public Editor getEditor() {
        return editor;
    }

    /**
     * Insert a hyperlink referring to a node. The hyperlink will refer to the UUID, if it is present, else to the path of the node.
     * 
     * @param range The range within the document, that shall be substituted by the hyperlink
     * @param info information about the hyperlink
     * @throws java.lang.Exception
     */
    public void insertInternalHyperlink(Range range, InternalHyperlinkInfo info) throws Exception {
        Element e = getRealParagraph(range.getStartOffset());
        if (getRealParagraph(range.getEndOffset()) != e)
            return; // can only insert as a span into a paragraph element

        // workaround for swing bug with p-implied paragraphs
        e = getParagraphWithNoImpliedSiblings(e);

        String hyperlink = null;
        String hrefValue = "";

        if (info.getReference() != null)
            hrefValue = REPOSITORY_URL + "?" +
                    NODE_REFERENCE + "=" + info.getReference();
        else
            hrefValue = REPOSITORY_URL + info.getPath();


        if (info.getValueProperty() != null && info.getValueProperty().length() > 0)
            hrefValue = HttpUtils.addParameter(hrefValue, Constants.PAR_PROPERTY_NAME, info.getValueProperty());

        if (info.getReference() != null)
            hyperlink = "<" + Tag.A.toString() + " " 
                    +  Attribute.HREF.toString() + "=\"" + hrefValue
                    +  "\">" + info.getValue() +
                    "</" + Tag.A.toString() + ">";
        else
            hyperlink = "<" + Tag.A.toString() + " " +
                    Attribute.HREF.toString() + "=\"" + REPOSITORY_URL + info.getPath() +
                    "\">" + info.getValue() +
                    "</" + Tag.A.toString() + ">";

        String html = substituteRange(e, hyperlink, range.getStartOffset(), range.getEndOffset());

        setOuterHTML(e, html);
    }

    /**
     * Insert a hyperlink to the internet.
     * 
     * @param range The range, that shall be substituted by the hyperlink
     * @param info information about the hyperlink
     */
    public void insertExternalHyperlink(Range range, ExternalHyperlinkInfo info) throws Exception {
        Element e = getRealParagraph(range.getStartOffset());
        if (getRealParagraph(range.getEndOffset()) != e)
            return; // can only insert as a span into a paragraph element

        String hyperlink = "<" + Tag.A.toString() + " " +
                Attribute.HREF.toString() + "=\"" + info.getUrl() +
                "\">" + info.getText() +
                "</" + Tag.A.toString() + ">";

        String html = substituteRange (e, hyperlink, range.getStartOffset(), range.getEndOffset());
        setOuterHTML(e, html);
    }
}
